package com.niuwa.p2p.action.borrowinfomg;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.apache.struts2.convention.annotation.Action;
import org.springframework.beans.factory.annotation.Autowired;

import com.niuwa.p2p.core.mvc.entity.JsonResult;
import com.niuwa.p2p.core.mvc.entity.PageBean;
import com.niuwa.p2p.core.utils.AppConstants;
import com.niuwa.p2p.core.utils.DictConstant.BorrowOperAuditType;
import com.niuwa.p2p.core.utils.DictConstant.LoanAuditStatus;
import com.niuwa.p2p.core.utils.StringUtil;
import com.niuwa.p2p.entity.business.LoanClaim;
import com.niuwa.p2p.entity.channel.LoanClaimAudit;
import com.niuwa.p2p.opr.utils.SessionUtil;
import com.niuwa.p2p.service.borrowinfomg.IBorrowInfoService;
import com.niuwa.p2p.service.channel.IChannelInfoService;
import com.niuwa.p2p.service.filesystem.FileSystem;
import com.niuwa.p2p.service.sequence.ISequenceService;
import com.niuwa.p2p.vo.borrowinfomg.LoanInfoVo;
import com.niuwa.p2p.vo.borrowinfomg.enterpriseinfomg.AppBorrowInfoVo;
import com.niuwa.p2p.vo.borrowinfomg.enterpriseinfomg.P2PBorrowInfoVo;

/**
 * 借款信息action
 * 
 * @author XiangChaoLei
 *
 */
public class BorrowInfoMgAction extends BorrowInfoMgCommonAction {

	private LoanInfoVo loanInfo;

	private List<LoanClaimAudit> auditLog;

	/**
	 * 
	 */
	private static final long serialVersionUID = -501319378127065059L;

	/** 日志 */
	private static Logger logger = Logger.getLogger(BorrowInfoMgAction.class);

	@Autowired
	private IChannelInfoService channelInfoService;
	@Autowired
	private IBorrowInfoService borrowInfoService;
	@Autowired
	private ISequenceService sequenceService;
	private String remark;
	@Autowired
	private FileSystem fileSystem;

	/**
	 * 跳转到借款项目信息列表
	 * 
	 * @return 借款项目信息列表视图
	 * 
	 */
	@Action("gotoP2PBorrowInfoList")
	public String gotoBorrowInfoList() {
		return "borrowinfoList";
	}

	/**
	 * 借款项目列表分页查询
	 * 
	 * @return
	 */
	@Action("listP2PBorrowinfo")
	public String BorrowinfoList() {
		// 获取Grid条件选择参数
		Map<String, Object> paramMap = getParamMap();
		// 执行查询操作
		PageBean<P2PBorrowInfoVo> pagination = borrowInfoService.searchP2PBorrowinfo(paramMap);
		// 设置返回
		setObjectReSult(pagination);
		return NONE;
	}

	/**
	 * 跳转到App借款项目信息列表
	 * 
	 * @return app借款项目信息列表视图
	 * 
	 */
	@Action("gotoAppBorrowInfoList")
	public String gotoAppBorrowInfoList() {
		return "AppborrowinfoList";
	}

	/**
	 * App借款项目列表分页查询
	 * 
	 * @return
	 */
	@Action("listAppBorrowinfo")
	public String AppBorrowinfoList() {
		// 获取Grid条件选择参数
		Map<String, Object> paramMap = getParamMap();
		// 执行查询操作
		PageBean<AppBorrowInfoVo> pagination = borrowInfoService.searchAppBorrowinfo(paramMap);
		// 设置返回
		setObjectReSult(pagination);
		return NONE;
	}

	/**
	 * 编辑
	 * 
	 * @throws Exception
	 */
	@Action("editBorrowInfo")
	public String editBorrowInfo() {
		this.setProjectOperateFlg(AppConstants.OPER_EDIT);
		String projectNo = getParam("projectNo");
		if (StringUtil.isEmpty(projectNo)) {
			setResult(new JsonResult());
			return "loanTab";
		}
		loanInfo = borrowInfoService.loadLoanInfo(projectNo);
		return "loanTab";
	}

	/**
	 * 查看
	 * 
	 * @throws Exception
	 */
	@Action("viewBorrowInfo")
	public String viewBorrowInfo() {
		this.setProjectOperateFlg(AppConstants.OPER_VIEW);
		String projectNo = getParam("projectNo");
		if (StringUtil.isEmpty(projectNo)) {
			setResult(new JsonResult());
			return "loanTab";
		}
		loanInfo = borrowInfoService.loadLoanInfo(projectNo);
		return "loanTab";
	}

	/**
	 * 审核
	 * 
	 * @return
	 */
	@Action("approveBorrowInfo")
	public String approveBorrowInfo() {
		this.setProjectOperateFlg(AppConstants.OPER_APPROVE);
		String projectNo = getParam("projectNo");
		if (StringUtil.isEmpty(projectNo)) {
			setResult(new JsonResult());
			return "loanTab";
		}
		loanInfo = borrowInfoService.loadLoanInfo(projectNo);
		return "loanTab";
	}

	/**
	 * 提交审核
	 * 
	 * @return
	 */
	@Action("toAuditBorrowInfo")
	public String toAuditBorrowInfo() {
		boolean success = false;
		String message = "操作失败";
		success = borrowInfoService.updateAuditStatus(LoanAuditStatus.AUDITING, this.getProjectNo(), SessionUtil
				.getSessionUser().getId(), BorrowOperAuditType.SUBMIT_AUDIT, remark);
		message = "操作成功";
		JsonResult result = new JsonResult();
		result.setSuccess(success);
		result.setMsg(message);
		setResult(result);
		return NONE;
	}

	/**
	 * 借款拒绝
	 * 
	 * @return
	 */
	@Action("toRejectBorrowInfo")
	public String RejectBorrowInfo() {
		boolean success = false;
		String message = "操作失败";
		success = borrowInfoService.updateAuditStatus(LoanAuditStatus.RETURN, this.getProjectNo(), SessionUtil
				.getSessionUser().getId(), BorrowOperAuditType.AUDIT_REJECT, remark);
		message = "操作成功";
		JsonResult result = new JsonResult();
		result.setSuccess(success);
		result.setMsg(message);
		setResult(result);
		return NONE;
	}

	/**
	 * 修改借款状态
	 * 
	 * @return
	 */
	@Action("toApproveBorrowInfo")
	public String toApproveBorrowInfo() {
		boolean success = false;
		String message = "操作失败";
		success = borrowInfoService.updateAuditStatus(LoanAuditStatus.PASS, this.getProjectNo(), SessionUtil
				.getSessionUser().getId(), BorrowOperAuditType.AUDIT_PASS, remark);
		message = "操作成功";
		JsonResult result = new JsonResult();
		result.setSuccess(success);
		result.setMsg(message);
		setResult(result);
		return NONE;
	}

	/**
	 * 显示借款的审核记录
	 * 
	 * @return
	 */
	@Action("showAuditLog")
	public String showAuditLog() {
		logger.info("showAuditLog action execute success");
		String projectNo = getParam("projectNo");
		if (StringUtil.isEmpty(projectNo)) {
			setResult(new JsonResult());
			return "loanAuditLog";
		}
		auditLog = borrowInfoService.loadLoanAuditLog(projectNo);
		return "loanAuditLog";
	}

	/**
	 * 检查申请金额是否大于可用金额
	 * 
	 * @return
	 */
	@Action("isChannelAmountUsable")
	public String isChannelAmountUsable() {
		String approveAmountStr = getParam("approveAmount");
		String channelCode = getParam("channelCode");
		if (StringUtil.isEmpty(approveAmountStr) || StringUtil.isEmpty(channelCode)) {
			setResult(new JsonResult(false));
			return NONE;
		}

		try {
			BigDecimal approveAmount = new BigDecimal(approveAmountStr.replaceAll(",", ""));

			BigDecimal usableAmount = borrowInfoService.loadChannelUsableAmount(channelCode);

			if (approveAmount.compareTo(usableAmount) > 0) {
				setResult(new JsonResult(false));
				return NONE;
			} else {
				setResult(new JsonResult(true));
				return NONE;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

		setResult(new JsonResult(false));
		return NONE;
	}

	/**
	 * 借款tab
	 * 
	 * @return
	 */
	@Action(value = "createLoan")
	public String createLoan() {
		logger.info("createLoan action execute success");
		this.setProjectOperateFlg(AppConstants.OPER_ADD);
		// 校验传入的企业代码
		String corpCode = getParam("corpCode");
		if (StringUtil.isEmpty(corpCode)) {
			setResult(new JsonResult());
			return NONE;
		}
		// 查询企业名称
		loanInfo = new LoanInfoVo();
		loanInfo.setLoanClaim(new LoanClaim());
		loanInfo.getLoanClaim().setBorrowerName(borrowInfoService.loadCorpName(corpCode));
		return "loanTab";
	}

	@Action("loadChannelInfo")
	public String loadChannelInfo() {
		logger.info("loadChannelInfo action execute success");
		setObjectReSult(channelInfoService.loadChannelInfo());
		return NONE;
	}

	@Action("saveLoanData")
	public String saveLoanData() {
		logger.info("saveLoanData action execute success");
		if (null == loanInfo || null == loanInfo.getLoanClaim()) {
			setResult(new JsonResult(false));
			return NONE;
		}
		// 判断是否存在了项目编号
		String projectNo = loanInfo.getLoanClaim().getProjectNo();
		// 新建项目
		if (StringUtil.isEmpty(projectNo)) {
			borrowInfoService.addLoanInfo(loanInfo);
		}
		// 保存
		else {
			borrowInfoService.updateLoanInfo(loanInfo);
		}
		JsonResult result = new JsonResult();
		result.setSuccess(true);
		result.setData(loanInfo.getLoanClaim().getProjectNo());
		result.setMsg("保存项目信息成功");
		setResult(result);
		return NONE;
	}

	public IChannelInfoService getChannelInfoService() {
		return channelInfoService;
	}

	public void setChannelInfoService(IChannelInfoService channelInfoService) {
		this.channelInfoService = channelInfoService;
	}

	public LoanInfoVo getLoanInfo() {
		return loanInfo;
	}

	public void setLoanInfo(LoanInfoVo loanInfo) {
		this.loanInfo = loanInfo;
	}

	public IBorrowInfoService getBorrowInfoService() {
		return borrowInfoService;
	}

	public void setBorrowInfoService(IBorrowInfoService borrowInfoService) {
		this.borrowInfoService = borrowInfoService;
	}

	public ISequenceService getSequenceService() {
		return sequenceService;
	}

	public void setSequenceService(ISequenceService sequenceService) {
		this.sequenceService = sequenceService;
	}

	public String getRemark() {
		return remark;
	}

	public void setRemark(String remark) {
		this.remark = remark;
	}

	public FileSystem getFileSystem() {
		return fileSystem;
	}

	public void setFileSystem(FileSystem fileSystem) {
		this.fileSystem = fileSystem;
	}

	public List<LoanClaimAudit> getAuditLog() {
		return auditLog;
	}

	public void setAuditLog(List<LoanClaimAudit> auditLog) {
		this.auditLog = auditLog;
	}
}
